<template>
  <div class="header">
    <div class="title">
      <el-icon><Key /></el-icon>
      <span>密码管理系统</span>
    </div>
    <div class="actions">
      <el-button type="primary" @click="handleGenerateKeys">
        <el-icon><RefreshRight /></el-icon>生成密钥对
      </el-button>
      <el-button type="danger" @click="handleLogout">
        <el-icon><SwitchButton /></el-icon>退出登录
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Key, RefreshRight, SwitchButton } from '@element-plus/icons-vue'
import request from '../utils/request'

const router = useRouter()

const handleGenerateKeys = async () => {
  try {
    await ElMessageBox.confirm('确定要生成新的密钥对吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    const res = await request.post('/api/keypair/generate')
    if (res.data.success) {
      ElMessage.success('密钥对生成成功')
    } else {
      ElMessage.error(res.data.message)
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('生成密钥对失败')
    }
  }
}

const handleLogout = () => {
  localStorage.removeItem('isSecondPassword')
  router.push('/login')
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.title {
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 8px;
}

.actions {
  display: flex;
  gap: 10px;
}
</style> 